package top.forforever.srb.core.controller.api;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.forforever.srb.common.result.R;
import top.forforever.srb.core.hfb.RequestHelper;
import top.forforever.srb.core.mapper.TransFlowMapper;
import top.forforever.srb.core.pojo.entity.LendItem;
import top.forforever.srb.core.pojo.vo.InvestVO;
import top.forforever.srb.core.service.LendItemService;
import top.forforever.srb.core.service.TransFlowService;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.TransformService;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Mr.Jiang
 * @since 2023-04-29
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class ApiLendItemController {

    @Autowired
    private LendItemService lendItemService;

    @Autowired
    private TransFlowService transFlowService;

    @PostMapping("auth/commitInvest")
    public R commitInvest(@RequestBody InvestVO investVO, HttpServletRequest request) {
        String investUserId = request.getHeader("userId");
        String form = lendItemService.commitInvest(investVO,investUserId);
        return R.ok().data("form",form);
    }

    @PostMapping("notify")
    public String investNotify(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> switchMap = RequestHelper.switchMap(parameterMap);
        String investNo = (String) switchMap.get("agentBillNo");
        // 幂等性判断，判断是否已投资信息
        Boolean transExit = transFlowService.isTransExit(investNo);
        if (transExit) {
            return "success";
        }
        lendItemService.investNotify(switchMap);
        return "succuss";
    }

}

